﻿using Xejen.Communication.Socket.Abstract;
using Xejen.Communication.Socket.Configuration;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace Xejen.Communication.Socket
{
    /// <summary>
    /// 创建以同步阻塞方式<see langword="Socket.Send"/>和<see langword="Socket.Receive"/>为驱动的Socket请求驱动器
    /// </summary>
    /// <creator>marc</creator>
    internal class SyncInvokerCreator : ISocketInvokerCreator<SyncInvoker>
    {
        private readonly ConcurrentDictionary<ConnectionConfig, SyncInvoker> _dictionary = new ConcurrentDictionary<ConnectionConfig, SyncInvoker>();
      
        /// <inheritdoc/>
        public SyncInvoker Create(ConnectionConfig config)
        {
            SyncInvoker request = new SyncInvoker(config);
            return request;
        }

        /// <inheritdoc/>
        public SyncInvoker CreateOrGet(ConnectionConfig config)
        {
            return _dictionary.GetOrAdd(config, (setting) =>
            {
                SyncInvoker request = new SyncInvoker(config);
                return request;
            });
        }
    }
}
